<?php
class Wow_Armory_Core_Url
{
    
    /**
     * Contains the configuration as object.
     * 
     * @var Wow_Armory_Config
     */
    protected $_config = null;
    
    /**
     * Includes the url as a string.
     * 
     * @var string
     */
    protected $_url = null;
    
    /**
     * Creates the object.
     * 
     * @param  Wow_Armory_Config $config
     * @return void
     */
    public function __construct(Wow_Armory_Config $config)
    {
        $this->_config = $config;
    }
    
    /**
     * Gets the configuration.
     * 
     * @return Wow_Armory_Config
     */
    public function getConfig()
    {
        return $this->_config;
    }
    
    /**
     * Overwrites the configuration.
     * 
     * @param  Wow_Armory_Config $config
     * @return Wow_Armory_Core_Url
     */
    public function overwriteConfig(Wow_Armory_Config $config)
    {
        $this->_config = $config;
        return $this;
    }
    
    /**
     * Gets the url as string.
     * 
     * @return string
     */
    protected function _getUrl()
    {
        if (!$this->getConfig()->isValid()) {
            throw new Wow_Armory_Exception('', 1016);
        }
        
        return sprintf(
            $this->getConfig()->getArmoryUrl(),
            $this->getConfig()->getLocation()
        );        
    }
    
    /**
     * Parse a url with some parameters.
     * 
     * @param  string $sheet
     * @param  string $character
     * @param  string $realm
     * @return string
     */
    protected function _getCharacterUrl($sheet, $character, $realm)
    {
        return $this->_getUrl() . '/' . $sheet . '?' . 
               $this->getConfig()->getRealmGetter() . '=' . $realm .
               '&' . $this->getConfig()->getCharacterNameGetter() . '=' . 
               $character;
    }
    
    /**
     * Parse the url and returns it.
     * 
     * @return string
     */
    public function getCharacterSheetUrl()
    {
        return $this->_getCharacterUrl(
            $this->getConfig()->getCharacterSheet(),
            $this->getConfig()->getCharacter()->getCharacterName(),
            $this->getConfig()->getRealm()->getRealmNameParsed()
        );
    }
    
    /**
     * Parse the talent url as a string.
     * 
     * @return string
     */
    public function getTalentSheetUrl()
    {
        return $this->_getCharacterUrl(
            $this->getConfig()->getTalentSheet(),
            $this->getConfig()->getCharacter()->getCharacterName(),
            $this->getConfig()->getRealm()->getRealmNameParsed()
        );
    }
    
    /**
     * Parse the statistic url as a string.
     * 
     * @return string
     */
    public function getStatisticSheetUrl()
    {
        return $this->_getCharacterUrl(
            $this->getConfig()->getStatisticSheet(),
            $this->getConfig()->getCharacter()->getCharacterName(),
            $this->getConfig()->getRealm()->getRealmNameParsed()
        );
    }
    
    /**
     * Gets the reputation sheet as url.
     * 
     * @return string
     */
    public function getReputationSheetUrl()
    {
        return $this->_getCharacterUrl(
            $this->getConfig()->getReputationSheet(),
            $this->getConfig()->getCharacter()->getCharacterName(),
            $this->getConfig()->getRealm()->getRealmNameParsed()
        );
    }
    
    /**
     * Returns the achievement sheet url.
     * 
     * @return string
     */
    public function getAchievementSheetUrl()
    {
        return $this->_getCharacterUrl(
            $this->getConfig()->getAchievementSheet(),
            $this->getConfig()->getCharacter()->getCharacterName(),
            $this->getConfig()->getRealm()->getRealmNameParsed()
        );
    }
    
    /**
     * Gets the guild sheet url as a string.
     * 
     * @return string
     */
    public function getGuildSheetUrl()
    {
        return $this->_getUrl() . '/' . $this->getConfig()->getGuildSheet() . '?' . 
               $this->getConfig()->getRealmGetter() . '=' . 
               $this->getConfig()->getRealm()->getRealmNameParsed() . '&' .
               $this->getConfig()->getGuildNameGetter() . '=' . 
               $this->getConfig()->getGuild()->getGuildName();
    }
    
}